home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / Documentation / Engineering Notes / Views / Using Dialogs < prev    next >
Encoding:
Text File  |  1996-08-16  |  9.2 KB  |  172 lines  |  [TEXT/ttxt]

  1. OpenDoc
  2. Development
  3. Framework
  4.                                                                                            
  5. Using Dialogs
  6. ODF Release 1                                                                                                                                                             
  7.  
  8.  
  9. Table of Contents
  10. ----------------------
  11. • Abstract
  12. • Modal Dialogs
  13.     • Basic Steps
  14.     • Handling Default and Cancel Buttons and Other Notifications
  15.     • Moveable and Non-moveable Dialogs
  16.     • Centering Dialogs on the Screen
  17.     • Dialogs with Text-edit Views
  18.     • Things that are Allowed in Modal Dialogs
  19.     • Things You Should Not Do in Modal Dialogs
  20. • Modeless Dialogs
  21. • Native Platform Alert Boxes
  22.  
  23.  
  24. Abstract
  25.  
  26. This Engineering note explains how to use the class FW_CDialogFrame  to implement dialog boxes with minimal work.   See also the document "About Box" in Engineering Notes:Miscellaneous:  which describes the subclass FW_CAboutFrame for adding a standard About box to your part.
  27.  
  28.  
  29. Modal Dialogs
  30.  
  31. Basic Steps
  32.  
  33. The basic steps to add a modal dialog to your part are:
  34. 1) Create a subclass of FW_CDialogFrame
  35. 2) Register a presentation for the dialog and create the dialog frame in YourPart::NewFrame.
  36. 3) Create the dialog views in your resource file, or in your code with the CreateSubViews method.
  37. 4) Add initialization code if necessary.
  38. 5) Add code to handle messages in your dialog's HandleNotification method.
  39.  
  40. The framework does the ground work for you:
  41. • It creates a new window with the style of your choice when you call NewModalDialog.
  42. • It creates a new instance of your DialogFrame class when opening the new window.
  43. • It sets OpenDoc's modal focus and disables the menu bar.
  44. • It handles keyboard binding for default and cancel buttons
  45. • It handles text-edit views.
  46.  
  47. Following is the code to open the Password dialog of the Form example.
  48.  
  49. void CFormFrame::OpenPasswordDialog(Environment* ev)
  50. {
  51.      FW_CPoint    position(FW_IntToFixed(400), FW_IntToFixed(200));
  52.      FW_CPoint    size(FW_IntToFixed(300), FW_IntToFixed(160));
  53.     
  54.      FW_CPresentation* dialogPresentation = GetFormPart()->GetPwdDialogPresentation();
  55.     
  56.      // ----- Create new dialog window with the "Password" presentation -----
  57.     
  58.      CPwdDialogFrame* dialog = (CPwdDialogFrame*) 
  59.        FW_CDialogFrame::NewModalDialog(ev, 
  60.                                                 GetPart(ev),                     // Your part
  61.                                                 dialogPresentation,     // Used in CFormPart::NewFrame
  62.                                                 size,                                  // Window size
  63.                                                 position,                           // Window position
  64.                                                 FW_kHasCaption,                  // Style to make dialog moveable
  65.                                                 FW_CString("Password"));    // Title for moveable dialog
  66.     
  67.       if (dialog != NULL)
  68.       {                        
  69.         dialog->Initialize(ev, this);        
  70.            dialog->GetWindow(ev)->Show(ev);
  71.       }
  72. }
  73.  
  74. Unlike the standard Mac Toolbox "ModalDialog" FW_CDialogFrame::NewModalDialog     returns right after creating the dialog.  Events are processed the same way as any other OpenDoc frames, except that the modal focus is set.  NewModalDialog can return NULL if the dialog creation failed because the     modal focus could not be granted.
  75.  
  76. After calling NewModalDialog you get a chance to initialize the dialog before opening the window yourself.  In this example Initialize is a private method of CPwdDialogFrame which simply sets a pointer back to the calling frame.  If you don't require any initialization code you can call  NewAndShowModalDialog which does the Show for you.
  77.  
  78. The dialog frame itself must be created by your part's NewFrame method like other frames, a dialog is another kind of presentation for the part.
  79.  
  80. FW_CFrame* CFormPart::NewFrame(...)    
  81. {
  82.   ...
  83.      else if (presentation == fPwdDialogPresentation) {
  84.            newFrame = (FW_CFrame*)FW_NEW(CPwdDialogFrame, 
  85.                                                        (ev, odFrame, presentation, this, kPasswordDialog));
  86.      }
  87.     
  88.      return newFrame;
  89. }
  90.  
  91. Note: In this example we pass the resource id kPasswordDialog to load the views from resource.  If you leave the id null you need to implement a CreateSubViews method in your dialog class.
  92.  
  93. While the modal dialog is visible the menu bar is disabled, except for the Edit menu if a text-edit view is active.
  94.  
  95. Handling Default and Cancel Buttons and Other Notifications
  96.  
  97. Usually your dialog will have a default button, "OK" or "Yes", and a cancel button, "Cancel" or "No".  The default button is drawn with a thick border to show that it responds to the <RETURN> or <ENTER> keys.  The cancel button responds to the <ESC> key or the standard Macintosh cancel command: <COMMAND> + <Period>.   Default and cancel buttons are created like other push buttons, in code or resources, and by setting their control message field to FW_kDefaultButtonMsg and FW_kCancelButtonMsg.
  98.  
  99. A FW_CDialogFrame object is also an instance of FW_MReceiver, so it can respond to notifications sent by its buttons or other notifiers.  FW_CDialogFrame responds to the default and cancel buttons by closing the dialog.  To add custom behavior or to respond to other controls , you must implement the HandleNotification method:
  100.  
  101. void CPwdDialogFrame::HandleNotification(Environment* ev, const FW_CNotification& notification)
  102. {
  103.        if (notification.GetMessage() ==  FW_kDefaultButtonMsg) 
  104.     {
  105.             // Handle the default button  
  106.             ...
  107.        } 
  108.    // Add code to handle other notifiers if necessary
  109.     
  110.       // Must call inherited to close the dialog on OK & Cancel
  111.       // WARNING: do not do anything else after this call... the dialog is gone!
  112.       FW_CDialogFrame::HandleNotification(ev, notification);
  113. }
  114.  
  115. The base class method FW_CDialogFrame::HandleNotification responds only to the default and cancel messages to dismiss the dialog.  If you wish to close it in response to another button (for instance a "No" button in a Yes/No/Cancel dialog) you can define a custom message for that button and do the following:
  116.  
  117.        if (notification.GetMessage() ==  kMyCloseMsg) 
  118.     {
  119.             // Close the dialog.  Do not do anything after this, the dialog is gone!   
  120.       GetWindow(ev)->CloseAndRemove(ev);
  121.       return;
  122.     }
  123.  
  124. To make a button notify the dialog, you need to call FW_CControl:LinkControlTo in code or define the control receiver field in resource.  See the document "Using Controls" for more information.
  125.  
  126. Moveable and Non-moveable Dialogs
  127.  
  128. The difference between creating a moveable and non-moveable modal dialog is only the window style.  Use the window style FW_kHasCaption to make the dialog moveable and pass a string for the window title.  Leave the window style as 0 to make the dialog non-moveable and pass an empty string FW_CString() for the title.  In general it's better to keep the dialog box moveable as this allows the user to switch to other processes.  
  129.  
  130. Centering Dialogs on the Screen
  131.  
  132. ODF will center the dialog box automatically if you pass a window style containing FW_kStandardDialogPosition. In the sample code above use the following style argument:
  133.  
  134.     FW_WindowStyle style = FW_kStandardDialogPosition | FW_kHasCaption;
  135.  
  136. Dialogs with Text-edit Views
  137.  
  138. FW_CDialogFrame adds a view tabber and an idler by default to handle text-edit views.  You don't need to remember to add them like you would in a FW_CFrame object.
  139.  
  140. Things that are Allowed in Modal Dialogs
  141.  
  142. • You are allowed to open another modal dialog above the current one.  ODF lets your dialog frame relinquish the modal focus as long as the new frame who wants it belongs to the same part.
  143.  
  144. • You are allowed to open a native platform alert box.
  145.  
  146. • You are allowed to support Undo/Redo in an edit view (you can use the CScrollEditView class of the Form example).  In this case it is recommended to clear the undo/redo action history when you delete the modal dialog.  See the comments in the constructor and destructor of CPwdDialogFrame in Form.
  147.  
  148. Things You Should Not Do in Modal Dialogs
  149.  
  150. • You should not try to execute any code after calling the base class FW_CDialogFrame::HandleNotification which will close and delete the dialog in response to the default and cancel buttons.
  151.  
  152. • You should not try to open a non-modal window . 
  153.  
  154. • You should not try to embed other parts in your dialog frame.
  155.  
  156.  
  157. Modeless Dialogs
  158.  
  159. Modeless dialog boxes are more like regular windows than modal dialog boxes.  They can be activated and deactivated,  and they don't need to be dismissed for your part to become active and editable.  
  160.  
  161. ODF 1 doesn't provide any special support for modeless dialogs.   In the future we plan to provide a way of sharing modeless dialogs between similar parts the same way floating windows can be shared now.
  162.  
  163.  
  164. Native Platform Alert Boxes
  165.  
  166. For simple alert boxes you don't need to use the FW_CDialogFrame class, you can open native alert windows directly with the  API declared in FWAlert.h:  FW_Alert, FW_NoteAlert, FW_QuestionAlert, FW_ErrorAlert.  These functions are documented in the Toolbox subsystem of the ODF Class Reference.
  167.  
  168. Alert boxes are system modal windows.  If a notification triggers an alert box, nothing else can be executed until the alert is closed, which can lead to problems if other notifications are waiting to be processed.  The work-around is to use a FW_CModalDialog  instead.
  169.  
  170.  
  171. © 1993 - 1996 Apple Computer, Inc. All rights reserved.
  172. Apple, the Apple Logo, Macintosh, and OpenDoc are trademarks of Apple Computer, Inc., registered in the United States and other countries.